<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        /* 全局样式 */
        :root {
            --primary-color: #165DFF;
            --secondary-color: #334155;
            --success-color: #00B42A;
            --danger-color: #F53F3F;
            --warning-color: #FF7D00;
            --light-color: #f7fafc;
            --dark-color: #1a202c;
            --border-color: #e2e8f0;
            --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s ease;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f8fafc;
            color: var(--dark-color);
            line-height: 1.6;
            min-height: 100vh;
            margin: 0;
            padding: 20px 20px 80px 20px; /* 增加底部内边距防止内容被页脚遮挡 */
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }

        .container {
            background-color: white;
            padding: 2.5rem;
            border-radius: 12px;
            box-shadow: var(--shadow);
            width: 100%;
            max-width: 500px;
            transition: var(--transition);
            margin: auto;
        }

        .container:hover {
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }

        h2 {
            text-align: center;
            color: var(--primary-color);
            margin-bottom: 2rem;
            font-weight: 700;
            font-size: 1.8rem;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            color: var(--secondary-color);
            font-weight: 500;
        }

        input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid var(--border-color);
            border-radius: 6px;
            box-sizing: border-box;
            font-size: 1rem;
            transition: var(--transition);
        }

        input:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(74, 108, 247, 0.1);
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 500;
            transition: var(--transition);
        }

        button:hover {
            background-color: #3a5bdb;
            transform: translateY(-2px);
        }

        .error-message {
            color: var(--danger-color);
            margin-top: 5px;
            font-size: 0.875rem;
        }

        .register-link {
            text-align: center;
            margin-top: 1.5rem;
        }

        .register-link a {
            color: var(--primary-color);
            text-decoration: none;
            font-weight: 500;
            transition: var(--transition);
        }

        .register-link a:hover {
            color: #3a5bdb;
            text-decoration: underline;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 1.5rem;
            }
        }
    
    /* 页脚样式 */
    footer {
        text-align: center;
        padding: 20px;
        color: var(--secondary-color);
        font-size: 0.9rem;
        border-top: 1px solid var(--border-color);
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        background-color: #f8fafc;
        z-index: 1000;
    }
</style>
</head>
<body>
    <div class="container">
        <h2>用户登录</h2>
        <form id="loginForm">
            <div class="form-group">
                <label for="phone">手机号</label>
                <input type="tel" id="phone" name="phone" placeholder="请输入手机号" required>
                <div class="error-message" id="phoneError"></div>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required>
                <div class="error-message" id="passwordError"></div>
            </div>
            <button type="submit">登录</button>
            <div class="register-link">
                还没有账号？<a href="register.html">立即注册</a>
            </div>
        </form>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            let isValid = true;

            // 手机号验证
            const phone = document.getElementById('phone').value;
            const phoneError = document.getElementById('phoneError');
            if (!/^1[3-9]\d{9}$/.test(phone)) {
                phoneError.textContent = '请输入有效的手机号';
                isValid = false;
            } else {
                phoneError.textContent = '';
            }

            // 密码验证
            const password = document.getElementById('password').value;
            const passwordError = document.getElementById('passwordError');
            if (password.length < 6 || password.length > 20) {
                passwordError.textContent = '密码长度应在6-20个字符之间';
                isValid = false;
            } else {
                passwordError.textContent = '';
            }

            if (isValid) {
                // 提交表单数据到后端
                fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        phone: phone,
                        password: password
                    })
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // 登录成功，根据用户角色跳转到不同页面
                        if (data.role === 'admin') {
                            window.location.href = 'server-management.html';
                        } else {
                            window.location.href = 'index.html';
                        }
                    } else {
                        alert('登录失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('登录请求失败:', error);
                    alert('登录请求失败，请稍后再试');
                });
            }
        });
    </script>
    <footer>
        权限管理工具@2025 | version <span id="versionInfo">加载中...</span> | 技术支持 liboi@Servyou.com.cn & wzj@Servyou.com.cn
    </footer>
    <script>
        // 获取并显示版本信息
        document.addEventListener('DOMContentLoaded', function() {
            fetch('/api/version')
                .then(response => response.json())
                .then(data => {
                    document.getElementById('versionInfo').textContent = data.version || '未知版本';
                })
                .catch(error => {
                    console.error('获取版本信息失败:', error);
                    document.getElementById('versionInfo').textContent = '获取失败';
                });
        });
    </script>
</body>
</html>